package com.car.advertising.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.car.advertising.R;
import com.car.advertising.entity.TaskRecordEntity;
import com.car.advertising.util.AmountCalculationUtil;
import com.car.advertising.view.CircleProgressView;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemLongClickListener;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 22:38 2017/4/23 04
 */

public class ProjectAdapter extends BaseRecyclerViewAdapter<TaskRecordEntity, BaseViewHolder> {

    public ProjectAdapter(Context context, List<TaskRecordEntity> projectListEntities) {
        super(context, projectListEntities);
    }

    public void refreshList(List<TaskRecordEntity> projectListEntities) {
        this.mDatas = projectListEntities;
        notifyDataSetChanged();
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, TaskRecordEntity model) {
        ProjectHolder projectHolder = (ProjectHolder) holder;
        if (!TextUtils.isEmpty(model.getTaskImg())) {
            Glide.with(mContext).load(model.getTaskImg()).into(projectHolder.iconIv);//
        } else {

        }
        projectHolder.nameTv.setText(model.getTaskTitle());
        projectHolder.briefTv.setText(model.getTotalPrice());
        projectHolder.profitTv.setText(model.getUnitPrice());
        projectHolder.cycleTv.setText(model.getTaskCycle());
        int percentage = AmountCalculationUtil.getPercentage(model.getPersonLimit(), model.getCurrentNum());
        projectHolder.progressCpv.setProgress(percentage);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_project_item, parent, false);
        return new ProjectHolder(mContext, view, this, getClickListener(), getLongClickListener());
    }

    class ProjectHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener clickListener;
        public OnRecyclerViewItemLongClickListener longClickListener;
        public ImageView iconIv;
        public TextView nameTv;
        public TextView briefTv;
        public TextView profitTv;
        public TextView cycleTv;
        public CircleProgressView progressCpv;

        public ProjectHolder(Context context, View rootView, RecyclerView.Adapter adapter, OnRecyclerViewItemClickListener clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
            super(context, rootView, adapter, clickListener, longClickListener);
            iconIv = (ImageView) rootView.findViewById(R.id.project_icon_iv);
            nameTv = (TextView) rootView.findViewById(R.id.project_name_tv);
            briefTv = (TextView) rootView.findViewById(R.id.project_brief_tv);
            profitTv = (TextView) rootView.findViewById(R.id.project_profit_tv);
            cycleTv = (TextView) rootView.findViewById(R.id.project_cycle_tv);
            progressCpv = (CircleProgressView) rootView.findViewById(R.id.project_progress_cpv);
            this.clickListener = clickListener;
            this.longClickListener = longClickListener;
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (clickListener != null) {
                clickListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (longClickListener != null) {
                longClickListener.onItemLongClick(parent, view, position);
            }
        }
    }
}
